<?php
header("Access-Control-Allow-Origin: *");
class IndexAction extends CommonAction {

    /**
     * 首页
     * @return void
     */
    public function index(){
        if( $this->is_mobile1() != false ){
            header('Location:/mobile/index.html');
        }else {
            //搜索类型，可选项有:0智能,1商标名,2注册号,3申请人
            $textType = $this->_get('st', 'intval', 0);
            //搜索模式 可选项有: 0至少包含一个字 1.开头包含 3.包含全部 4.精准匹配 默认是3
            $searchMode = $this->_get('sm', 'intval', 3);
            //搜索内容
            $text = $this->_get('k', 'trim');
            //同名多类
            $md = $this->_get('md', 'trim');
            //
            //默认搜索参数
            $this->assign('initSearcher', json_encode(['textType' => $textType, 'searchMode' => $searchMode, 'text' => $text, 'md' => $md, 'sqNum' => '1-90']));
            $this->assign('initCategory', json_encode($this->getCategory()));
            $this->assign('md', $md);
            $this->display('Indexnew:index');
        }
    }

    /**
     * AJAX 搜索
     * @return JSON
     */
    public function search(){
        //分页参数
        $pageCurrent = $this->_post('current', 'intval');
        $pageLimit = $this->_post('limit', 'intval');
        $url = 'https://biaobiaoku.com/api/search?';
        $param = array();
        $param["ip"] = empty($_SERVER["REMOTE_ADDR"]) ? "47.102.187.210" : $_SERVER["REMOTE_ADDR"];
        $param["current"] = $pageCurrent;
        $param["limit"] =$pageLimit;
        //搜索类型，可选项有:0智能,1商标名,2注册号,3申请人
        $st = $this->_post("textType");
        if( $st == 0 || $st == "智能" ){
            $param["textType"] = '智能';
        }elseif( $st == 1 || $st == "商标名" ){
            $param["textType"] = '商标名';
        }elseif( $st == 2 || $st == "注册号" ){
            $param["textType"] = '注册号';
        }elseif( $st == 3 || $st == "申请人" ){
            $param["textType"] = '申请人';
        }else{
            $param["textType"] = '智能';
        }
        //搜索模式 可选项有: 0至少包含一个字 1.开头包含 3.包含全部 4.精准匹配 默认是3
        $sm = $this->_post("searchMode",'trim');
        if( $sm == '' ){
            $param["searchMode"] = 3;
        }else if($sm == 0 ){
            $param["searchMode"] = 0;
        }elseif( $sm == 1 ){
            $param["searchMode"] = 1;
        }elseif( $sm == 3 ){
            $param["searchMode"] = 3;
        }elseif( $sm == 4 ){
            $param["searchMode"] = 4;
        }else{
            $param["searchMode"] = 3;
        }
        //搜索内容
        $text = $this->_post("text",'trim');
        if( !empty($text) ){
            $param["text"] = $text;
        }
        //价格由低到高,排序规则，1.价格由低到高 2.价格由高到低 3.新上传的在前面 4.新上传的在后面 5.星星由高到低 0.默认排序
        $orderBy = $this->_post("orderBy", "intval");
        if( $orderBy == 1 ){
            $param["orderCode"] = 1;
        }else{
            $param["orderCode"] = 0;
        }
        //同名标仅显示一个类别
        $distinct = $this->_post("distinct", "intval");
        if( $distinct == 1 ){
            $param["distinct"] = true;
        }else{
            $param["distinct"] = false;
        }
        $priceStart = $this->_post("priceStart","intval");
        if( $priceStart > 0 ){
            $newprice = intval($priceStart * 100 / ( 100 + $this->buyFasterRate ));
            if( $priceStart - $newprice > $this->buyFasterHigh ){
                $param["priceStart"] = $priceStart - $this->buyFasterHigh - 0;
            }elseif( $priceStart - $newprice < $this->buyFasterLow ){
                $param["priceStart"] = $priceStart - $this->buyFasterLow - 0;
            }else{
                $param["priceStart"] = $newprice - 0;
            }
        }
        $priceEnd = $this->_post("priceEnd","intval");
        if( $priceEnd > 0 ){
            $newprice = intval($priceEnd * 100 / ( 100 + $this->buyFasterRate ));
            if( $priceEnd - $newprice > $this->buyFasterHigh ){
                $param["priceEnd"] = $priceEnd - $this->buyFasterHigh - 0;
            }elseif( $priceEnd - $newprice < $this->buyFasterLow ){
                $param["priceEnd"] = $priceEnd - $this->buyFasterLow - 0;
            }else{
                $param["priceEnd"] = $newprice - 0;
            }
            //屏蔽3万以上的标的
            if( $param["priceEnd"] > 18000 ){
                $param["priceEnd"] = 18000;
            }
        }else{
            //屏蔽3万以上的标的
            $param["priceEnd"] = 18000;
        }
        //注册年限
        $ageStart = $this->_post("ageStart"); //开始时间
        if( $ageStart != "" ){
            $param["ageStart"] = $ageStart;
        }
        $ageEnd = $this->_post("ageEnd"); //结束时间
        if( $ageEnd > 0 ){
            $param["ageEnd"] = $ageEnd;
        }
        //商标类别 array
        $leis = $_POST["leis"];
        if( is_array($leis) ) {
            foreach ($leis as $k => $v) $leis[$k] = intval($v);
            $leiStr = implode(",", $leis);
            $param["leis"] = $leiStr;
        }
        //类似群组 array
        $teams = $_POST["teams"];
        if( is_array($teams) ){
            foreach ($teams as $k => $v) $teams[$k] = htmlspecialchars($v);
            $teamStr = implode(",", $teams);
            $param["teams"] = $teamStr;
        }
        //产品/服务
        $goods = $_POST["goods"];
        if( is_array($goods) ){
            $param["goodsName"] = implode(" ", $goods);
        }
        //组合类型 array
        $types = $_POST["types"];
        if( is_array($types) ){
            $param["types"] = implode(",", $types);
        }
        //商标字数 array
        $nameSize = $_POST["nameSize"];
        if( is_array($nameSize) ){
            $param["nameSizeList"] = implode(",", $nameSize);
        }
        //名称结构
        $abTypes = $_POST["abTypes"];
        if( is_array($abTypes) ){
            $param["abTypes"] = implode(",", $abTypes);
        }
        //申请人类型 1、公司，2、个人
        $sqType = $this->_post("sqType", "intval");
        if( $sqType == 1 || $sqType == 2 ){
            $param["sqType"] = $sqType;
        }
        //注册人申请量 1-80 【String】
        $sqNum = "1-80";//$this->_post("sqNum");
        if( !empty($sqNum) ){
            $param["sqNum"] = $sqNum;
        }
        //同名多类
        $md = $this->_post("md");
        if( !empty($md) ){
            $param["md"] = $md;
        }
        $url = $url . http_build_query($param);
        //缓存初始化条件
        $cacheKey = "nonaodashi_mobile_" . md5($url);
        $cacheValue = $this->redis_cache->get($cacheKey);
        if( !empty($cacheValue) ){
            echo $cacheValue;
            exit;
        }
        $res = file_get_contents($url);
        $response = json_decode($res, true, JSON_UNESCAPED_UNICODE);
        $code = $response['code'];
        $msg = $response['msg'];
        if ($code == 0) {
            $total = $response['data']['total'];
            $list = $response['data']['results'];
            if (!empty($list)) {
                $vos = array();
                foreach ($list as $k => $v) {
                    $vos[$k]['name'] = $v['name'];
                    //计算最新价格
                    $newprice = intval($v['price'] * (100 + $this->buyFasterRate) / 100);
                    if( $newprice < ($v['price'] + $this->buyFasterLow) ){
                        $vos[$k]['price'] = $v['price'] + $this->buyFasterLow;
                    }else if( $newprice > ($v['price'] + $this->buyFasterHigh) ){
                        $vos[$k]['price'] = $v['price'] + $this->buyFasterHigh;
                    }else{
                        $vos[$k]['price'] = $newprice;
                    }
                    $vos[$k]['lei'] = $v['lei'];
                    $vos[$k]['id'] = $v['id'];
                    //$vos[$k]['thumb_image'] = "https://color.picer.cdn.biaobiaoku.com/" . $v['brandId'] . "-" . $v['picHash'] . "-250.png";
                    //$vos[$k]['trademark_image'] = "https://brand.picer.cdn.biaobiaoku.com/" . $v['brandId'] . "-" . $v['picHash'] . ".jpg";
                    $vos[$k]['thumb_image'] = $this->getImageUrl($v,2);
                    $vos[$k]['trademark_image'] = $this->getImageUrl($v,0);
                    $vos[$k]['count_goods_service'] = $v['xxNum'];
                    $vos[$k]['lei_name'] = $this->getCategory($v['lei']);
                }
            }
            //响应
            $result = ['code' => 0, 'msg' => '', 'ip' => $_SERVER["REMOTE_ADDR"], 'data' => ['text' => $msg, 'total' => $total, 'results' => $vos]];
            $cacheValue = json_encode($result);
            $this->redis_cache->set($cacheKey, $cacheValue, 2);
            echo $cacheValue;
            exit;
        }
        echo json_encode(['code' => 0, 'msg' => $msg, 'ip' => $_SERVER["REMOTE_ADDR"], 'data' => ['total' => 0, 'results' => []]]);
        exit;
    }

    /**
     * @return void
     */
    public function getkefu(){
        $data = array();
        $data['kefu_mobile'] = "13838111503";
        $data['kefu_qrcode_url'] = 'https://naonaodashi.com/static/img/guwen.jpg?v=20240707';
        echo json_encode(['code' => 0, 'msg' => "", 'data' => $data]);
        exit;
    }

    function is_mobile1(){
        //匹配的手机型号
        $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
        $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
        $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
        $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
        $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
        $regex_match.=")/i";
        if( isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])) ){
            return true;
        }
        if( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !==false ){
            //兼容微信内置浏览器，PC访问
            if(  strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !==false or strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !==false ){
                return false;
            }
            return true;
        }
        return false;
    }
}